// pages/detail/detail.js

const app = getApp();
var WxParse = require('../../../../wxParse/wxParse.js');

Page({

  /**
   * 页面的初始数据
   */
  data: {
    newindex: 0,  //当前轮播的index
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    const item_id = options.item_id;
    wx.request({
      url: app.baseURL + 'Integral&a=item_detail',
      data: {
        user_id: app.globalData.userId,
        item_id: item_id
      },
      success: (res) => {
        if (res.statusCode == 200) {
          WxParse.wxParse('goodsinfo', 'html', res.data.item_info.info, this, 0);  //调用wxParse解析html文本
          this.setData({
            integralData: res.data.item_info,
            total_integral: res.data.total_integral
          })
        }
      }
    })
  },
  redeemNow(){
    wx.showLoading({title: '加载中...'});
    const total_integral = Number(this.data.total_integral);
    const integral = Number(this.data.integralData.integral);
    if (total_integral >= integral){
      const data = {
        type: 1,
        item_id: this.data.integralData.id,
        item_num: 1
      }
      let info = JSON.stringify(data);
      wx.navigateTo({
        url: '../integralPlaceorder/integralPlaceorder?info=' + info
      })
    }else{
      wx.showToast({
        title: '可用积分不足',
        icon: "none"
      })
    }
  },

  //获取当前轮播的index
  bindchange (e) {
    console.log(e.detail.current);
    this.setData({
      newindex: e.detail.current
    })
  },

  // 预览图片
  previewImage () {
    let newindex = this.data.newindex;
    wx.previewImage({
      current: this.data.integralData.img[newindex],
      urls: this.data.integralData.img,
    })
  }
})